package cc.sunni.article.controller;

import cc.sunni.api.BaseController;
import cc.sunni.api.controller.article.ArticleControllerApi;
import cc.sunni.api.utils.PageUtils;
import cc.sunni.article.service.ArticleService;
import cc.sunni.common.Constant;
import cc.sunni.common.enums.ArticleCoverType;
import cc.sunni.common.enums.ArticleReviewStatus;
import cc.sunni.common.enums.YesOrNo;
import cc.sunni.common.utils.JsonUtils;
import cc.sunni.common.utils.R;
import cc.sunni.common.utils.ResultEnum;
import cc.sunni.pojo.Category;
import cc.sunni.pojo.bo.NewArticleBO;
import cc.sunni.pojo.vo.ArticleDetailVO;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.mongodb.client.gridfs.GridFSBucket;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.IOUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ArticleController extends BaseController implements ArticleControllerApi {
    @Autowired
    private ArticleService articleService;

    @Override
    public R createArticle(@Validated NewArticleBO newArticleBO) {
        // 判断文章封面类型，单图必填，纯文字则设置为空
        if (newArticleBO.getArticleType().equals(ArticleCoverType.ONE_IMAGE.type)) {
            if (StringUtils.isBlank(newArticleBO.getArticleCover())) {
                return R.en(ResultEnum.ARTICLE_COVER_NOT_EXIST_ERROR);
            }
        } else if (newArticleBO.getArticleType().equals(ArticleCoverType.WORDS.type)) {
            newArticleBO.setArticleCover("");
        }

        // 判断分类id是否存在
        String allCatJson = redis.get(Constant.REDIS_ALL_CATEGORY);
        if (StringUtils.isBlank(allCatJson)) {
            return R.en(ResultEnum.SYSTEM_OPERATION_ERROR);
        } else {
            List<Category> catList = JSONUtil.toList(allCatJson, Category.class);
            boolean anyMatch = catList.stream().parallel().noneMatch(e -> e.getId().equals(newArticleBO.getCategoryId()));
            if (anyMatch) {
                return R.en(ResultEnum.ARTICLE_CATEGORY_NOT_EXIST_ERROR);
            }
        }
        articleService.createArticle(newArticleBO);
        return R.ok();
    }

    @Override
    public R queryMyList(String userId, String keyword, Integer status, Date startDate, Date endDate, Integer page, Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return R.en(ResultEnum.ARTICLE_QUERY_PARAMS_ERROR);
        }
        // 查询我的列表，调用service
        PageUtils grid = articleService.queryMyArticleList(userId, keyword, status, startDate, endDate, page, pageSize);
        return R.ok(grid);
    }

    @Override
    public R queryAllList(Integer status, Integer page, Integer pageSize) {
        PageUtils gridResult = articleService.queryAllArticleListAdmin(status, page, pageSize);
        return R.ok(gridResult);
    }

    @Override
    public R doReview(String articleId, Integer passOrNot) {
        Integer pendingStatus;
        if (passOrNot.equals(YesOrNo.YES.type)) {
            // 审核成功
            pendingStatus = ArticleReviewStatus.SUCCESS.type;
            //
        } else if (passOrNot.equals(YesOrNo.NO.type)) {
            // 审核失败
            pendingStatus = ArticleReviewStatus.FAILED.type;
        } else {
            return R.en(ResultEnum.ARTICLE_REVIEW_ERROR);
        }
        // 保存到数据库，更改文章的状态为审核成功或者失败
        articleService.updateArticleStatus(articleId, pendingStatus);
        return R.ok();
    }

    @Override
    public R delete(String userId, String articleId) {
        articleService.deleteArticle(userId, articleId);
        return R.ok();
    }

    @Override
    public R withdraw(String userId, String articleId) {
        articleService.withdrawArticle(userId, articleId);
        return R.ok();
    }

}
